window.onload = function(){
	llgd.changePage(function(){
		leftSwipe();
		rightSwipe();
	});	
	
}
//args.swipeDom 大容器对象  args.swipeType 滑动类型  args.swipeDistance 缓冲距离

function leftSwipe(){
	var parentBox = document.querySelector('.cat_l');
	var childBox = parentBox.querySelector('ul');
	var parentHeight = parentBox.offsetHeight;
	var childHeight = childBox.offsetHeight;
	var maxY = 0;
	var minY = parentHeight-childHeight;
	llgd.iScroll({
		swipeDom:parentBox,
		swipeType:'y',
		swipeDistance:100,

	})
	
	// 公共方法
	var setTranslateY = function(y){
		childBox.style.webkitTransform = "translateY("+y+"px)";
		childBox.style.transform = "translateY("+y+"px)";
	}
	var  setTransition = function(){
		childBox.style.webkitTransition = "all 0.2s";
		childBox.style.transition = "all 0.2s";
	}
	var removeTransition=function(){
		childBox.style.webkitTransition = "none";
		childBox.style.transition = "none";
	}
	touchLi(childBox,function(e){
		var lis= childBox.children;
		var li = e.target.parentNode;
		for (var i = 0; i < lis.length; i++) {
			lis[i].className = "";
			lis[i].index = i;
		}
		li.className = 'now';
		translateY = -li.index*50;
		currY = translateY;
		if (currY < minY) {
			currY = minY;
		}
		setTransition();
		setTranslateY(currY);
	})
	

}
// function rightSwipe(){
// 	// 点击菜单时改变样式
// 	// 点击菜单 定位到当前的那个菜单顶部 如果不满足定位区间就不足滑动
// 	var parentBox = document.querySelector('.cat_r');
// 	var childBox = parentBox.querySelector('.goods_box');
// 	var parentHeight = parentBox.offsetHeight;
// 	var childHeight = childBox.offsetHeight;
// 	var maxY = 0;
// 	var minY = parentHeight-childHeight;
// 	var distance = 100;
// 	var maxSwipe = maxY + 100;
// 	var minSwipe = minY - 100;
// 	// 公共方法

// 	var setTranslateY = function(y){
// 		childBox.style.webkitTransform = "translateY("+y+"px)";
// 		childBox.style.transform = "translateY("+y+"px)";
// 	}
// 	var  setTransition = function(){
// 		childBox.style.webkitTransition = "all 0.2s";
// 		childBox.style.transition = "all 0.2s";
// 	}
// 	var removeTransition=function(){
// 		childBox.style.webkitTransition = "none";
// 		childBox.style.transition = "none";
// 	}
// 	var startY = 0;
// 	var moveY = 0;
// 	var distanceY = 0;
// 	var isMove = false;
// 	var currY = 0;

// 	// 菜单可滑动
// 	childBox.addEventListener('touchstart', function(e){
// 		startY = e.touches[0].clientY;
// 	})
// 	childBox.addEventListener('touchmove',function(e){
// 		isMove = true;
// 		moveY = e.touches[0].clientY;
// 		distanceY = moveY-startY;
// 		removeTransition();

// 	// 滑动有范围
// 		if (currY+distanceY > maxSwipe || currY+distanceY < minSwipe) {
// 			return;
// 		}
// 		setTranslateY(currY+distanceY);
// 	})
// 	window.addEventListener('touchend',function(e){
// 	// 触摸结束时，需要判断是否在给定的区间内，否则吸附回去
// 		currY = currY + distanceY;
// 		if (currY>maxY) {
// 			currY = maxY;
// 		}else if(currY<minY){
// 			currY = minY;
// 		}
// 		setTransition();
// 		setTranslateY(currY);

// 		startY = 0;
// 		moveY=0;
// 		distanceY = 0;
// 		isMove = false;
// 	})
// }
function rightSwipe(){
	llgd.iScroll({
		swipeDom:document.querySelector('.cat_r'),
		swipeType:'y',
		swipeDistance:100
	})
}
function touchLi(dom,callback){
	if(dom && typeof dom =="object"){
		var isMove = false;
		var startTime = 0;
		var startX = 0;
		var endX = 0;

		dom.addEventListener('touchstart', function(e){
			startTime = Date.now();
			if(e.touches.length>1){
				return;
			}
		})
		dom.addEventListener('touchmove',function(e){
			isMove=true;
		})
		dom.addEventListener('touchend',function(e){
			if (!isMove && (Date.now()-startTime) <150) {
				callback&&callback(e);
			}
			startTime = 0;
			isMove =false;
		})
	}
	
}